/**
 *
 *  加法
 * @param {Number} a
 * @param {Number} b
 * @returns a+b的值
 */
const add = (a, b, ...arr) => {
  if (!b) return a
  var c, d, e
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  e = Math.pow(10, Math.max(c, d))
  const AND = (mul(a, e) + mul(b, e)) / e
  if (arr.length === 0) return AND
  return add(AND, ...arr)
}

/**
 *
 * 减法
 * @param {Number} a
 * @param {Number} b
 * @returns a-b的值
 */
const sub = (a, b) => {
  var c, d, e
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  return (e = Math.pow(10, Math.max(c, d))), (mul(a, e) - mul(b, e)) / e
}

/**
 *
 * 乘法
 * @param {Number} a
 * @param {Number} b
 * @returns a*b的值
 */
const mul = (a, b) => {
  var c = 0
  var d = a.toString()
  var e = b.toString()
  try {
    c += d.split('.')[1].length
  } catch (f) {}
  try {
    c += e.split('.')[1].length
  } catch (f) {}
  return (
    (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
  )
}

/**
 *
 * 除法
 * @param {Number} a
 * @param {Number} b
 * @returns
 */
const div = (a, b) => {
  var c
  var d
  var e = 0
  var f = 0
  try {
    e = a.toString().split('.')[1].length
  } catch (g) {}
  try {
    f = b.toString().split('.')[1].length
  } catch (g) {}
  return (
    (c = Number(a.toString().replace('.', ''))),
    (d = Number(b.toString().replace('.', ''))),
    mul(c / d, Math.pow(10, f - e))
  )
}
const randomNum = (n) => {
  var t='';
  for(var i=0;i<n;i++) {
    t+=Math.floor(Math.random()*10);
  }
  return t;
}


module.exports = {
  add,
  randomNum
}